package com.pig4cloud.pig.module.service.property.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.TransactionalException;
import com.pig4cloud.pig.common.core.util.OrderUtil;
import com.pig4cloud.pig.common.core.wx.WxAppPay;
import com.pig4cloud.pig.module.api.entity.platform.PlatformPay;
import com.pig4cloud.pig.module.api.entity.property.PropertyFeeOrder;
import com.pig4cloud.pig.module.mapper.property.PropertyFeeOrderMapper;
import com.pig4cloud.pig.module.service.platform.IPlatformPayService;
import com.pig4cloud.pig.module.service.property.IPropertyFeeOrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 物业费订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Service
public class PropertyFeeOrderServiceImpl extends ServiceImpl<PropertyFeeOrderMapper, PropertyFeeOrder> implements IPropertyFeeOrderService {
	@Resource
	private PropertyFeeOrderMapper propertyFeeOrderMapper;
	@Resource
	private IPlatformPayService platformPayService;

	@Value("${web.wx_refund_notify_url}")
	private String wx_refund_notify_url;//微信支付的回调地址

	@Value("${web.appid}")
	private String appid;
	@Value("${web.secret}")
	private String secret;
	@Value("${web.mch_id}")
	private String mch_id;
	@Value("${web.api_key}")
	private String api_key;
	@Value("${web.keyPath}")
	private String keyPath;//微信支付的回调地址

	@Override
	public PropertyFeeOrder selectOrderTongjiByEstateId(Map<String, Object> map) {
		return propertyFeeOrderMapper.selectOrderTongjiByEstateId(map);
	}

	@Override
	@Transactional
	public int refundOrder(PropertyFeeOrder feeOrder) {
		int ret = 0;
		try{
			PlatformPay platformPay = platformPayService.findById(feeOrder.getPayPlatformId());
			//订单状态   0 - 交费申请 1 - 已生成交费清单    2 - 待支付  3- 支付中  4 - 已支付 5 - 订单取消   6 - 已发起退款  7 - 退款成功 8 - 退款失败
//			feeOrder.setState(7);
			double leftMoney = feeOrder.getPayMoney().doubleValue();
			String out_refund_no = OrderUtil.getOrderNoByTimespan();
			if(feeOrder.getPayPlatform() == 1){
				out_refund_no = platformPay.getSysCode() + OrderUtil.getOrderNoByTimespan();
			}
			feeOrder.setRefundOutTradeNo(out_refund_no);

			//支付平台  0 - 微信支付 1 - 农商行支付
			if (feeOrder.getRefundStatus() == 0 || feeOrder.getRefundStatus() == 1 || feeOrder.getRefundStatus() == 3) {
				if (leftMoney > 0) {
					String retRequest = "";
					if(feeOrder.getPayPlatform() == 0) {
						WxAppPay wxAppPay = new WxAppPay(wx_refund_notify_url, secret, appid, mch_id, api_key);
						retRequest = wxAppPay.refundPayAPIStr(feeOrder.getOutTradeNo(), feeOrder.getPayMoney(), new BigDecimal(leftMoney),
							feeOrder.getRefundOutTradeNo(), keyPath);
					} else {
						retRequest = platformPayService.refundOrder(feeOrder.getOutTradeNo(), new BigDecimal(leftMoney),feeOrder.getRefundOutTradeNo(),platformPay);
						System.out.println("retRequest: " + retRequest);
					}

					JSONObject jsonObject = JSONUtil.parseObj(retRequest);
					if (jsonObject != null) {
						String code = "";
						String msg = "";
						boolean bl = false;
						if(feeOrder.getPayPlatform() == 0) {
							code = jsonObject.containsKey("code") ? jsonObject.getStr("code") : "";
							msg = jsonObject.containsKey("msg") ? jsonObject.getStr("msg") : "";

							if (code.equals("SUCCESS")) {
								bl = true;
							}
						}else{
							code = jsonObject.getStr("errCode");
							msg = jsonObject.getStr("errMsg");
							String refundStatus = jsonObject.getStr("refundStatus");

							if (code.equals("SUCCESS") && refundStatus.equals("SUCCESS")) {
								bl = true;
							}
						}

						if (bl) {
							//订单状态   0 - 交费申请 1 - 已生成交费清单    2 - 待支付  3- 支付中  4 - 已支付 5 - 订单取消   6 - 已发起退款  7 - 退款成功 8 - 退款失败
							feeOrder.setState(6);
							//0 未退款 1 申请中 2 已退款
							feeOrder.setRefundStatus(1);
						} else {
							//订单状态   0 - 交费申请 1 - 已生成交费清单    2 - 待支付  3- 支付中  4 - 已支付 5 - 订单取消   6 - 已发起退款  7 - 退款成功 8 - 退款失败
							feeOrder.setState(8);
							//0 未退款 1 申请中 2 已退款  3 - 失败
							feeOrder.setRefundStatus(3);
						}
						feeOrder.setRefundReason(msg);
					}

					ret = propertyFeeOrderMapper.updateById(feeOrder);
					if(ret <= 0){
						throw new TransactionalException("chargerOrderMapper updateById error");
					}
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return ret;
	}
}
